package com.dyzx.hbb.modules.fcxs.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseController;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.common.utils.DateFormat;
import com.dyzx.hbb.modules.fcxs.entity.FcxsDiscount;
import com.dyzx.hbb.modules.fcxs.entity.FcxsFinancial;
import com.dyzx.hbb.modules.fcxs.entity.FcxsOrder;
import com.dyzx.hbb.modules.fcxs.entity.FcxsPaymentPlan;
import com.dyzx.hbb.modules.fcxs.query.FcxsPaymentPlanQuery;
import com.dyzx.hbb.modules.fcxs.service.FcxsOrderService;
import com.dyzx.hbb.modules.fcxs.service.FcxsPaymentPlanService;
import com.dyzx.hbb.modules.fcxs.vo.FcxsPaymentPlanUVO;
import com.dyzx.hbb.modules.fcxs.vo.FcxsPaymentPlanVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 缴费计划管理控制器
 * 
 * 功能说明：负责处理房地产销售系统中缴费计划的创建、查询、更新和删除等操作
 * 主要职责：
 * - 提供缴费计划的CRUD接口
 * - 支持按条件查询缴费计划列表
 * - 处理缴费计划添加和编辑时的订单关联验证
 * - 支持按订单ID、类型、状态、日期等条件筛选缴费计划数据
 * - 关联展示订单信息和操作人信息
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "03.缴费计划", description = "缴费计划相关接口")
@RestController
@RequestMapping("/fcxs/payment_plan")
public class FcxsPaymentPlanController extends BaseValidController<
        FcxsPaymentPlanService,
        FcxsPaymentPlan,
        FcxsPaymentPlanQuery,
        FcxsPaymentPlanVO,
        FcxsPaymentPlanUVO
        > {
    @Autowired
    private FcxsPaymentPlanService fcxsFinancialService;

    @Autowired
    private FcxsOrderService fcxsOrderService;

    @Override
    protected FcxsPaymentPlanService getBaseService() {
        return fcxsFinancialService;
    }

    @Override
    protected MyLambdaQueryWrapper<FcxsPaymentPlan> queryWrapper(FcxsPaymentPlanQuery query) {
        MyLambdaQueryWrapper<FcxsPaymentPlan> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(FcxsPaymentPlan::getId)
                .select(FcxsPaymentPlan::getOrderId)
                .fcxsOrderNo(FcxsPaymentPlan::getOrderId).as("order_no")
                .select(FcxsPaymentPlan::getType)
                .select(FcxsPaymentPlan::getAmt)
                .select(FcxsPaymentPlan::getStatus)
                .select(FcxsPaymentPlan::getRemark)
                .select(FcxsPaymentPlan::getPayDate)
                .select(FcxsPaymentPlan::getCreateDate)
                .select(FcxsPaymentPlan::getUpdateDate)
                .organName(FcxsPaymentPlan::getOrganId).as("organ_name")
                .userName(FcxsPaymentPlan::getOperatorId).as("operator_name");
        if (query != null) {
            queryWrapper.eq(query.getOrder_id() != null, FcxsPaymentPlan::getOrderId, query.getOrder_id())
                    .findInSet(StringUtils.isNotBlank(query.getType()), FcxsPaymentPlan::getType, query.getType())
                    .findInSet(StringUtils.isNotBlank(query.getStatus()), FcxsPaymentPlan::getStatus, query.getStatus())
                    .dateBetween(StringUtils.isNotBlank(query.getCreate_date()), FcxsPaymentPlan::getCreateDate, DateFormat.DATE, query.getCreate_date());
        }
        return queryWrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<FcxsPaymentPlan> queryInfoWrapper() {
        MyLambdaQueryWrapper<FcxsPaymentPlan> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(FcxsPaymentPlan::getId)
                .select(FcxsPaymentPlan::getOrderId)
                .fcxsOrderNo(FcxsPaymentPlan::getOrderId).as("order_no")
                .select(FcxsPaymentPlan::getType)
                .select(FcxsPaymentPlan::getAmt)
                .select(FcxsPaymentPlan::getStatus)
                .select(FcxsPaymentPlan::getRemark)
                .select(FcxsPaymentPlan::getPayDate)
                .select(FcxsPaymentPlan::getCreateDate)
                .select(FcxsPaymentPlan::getUpdateDate)
                .organName(FcxsPaymentPlan::getOrganId).as("organ_name")
                .userName(FcxsPaymentPlan::getOperatorId).as("operator_name");
        return queryWrapper;
    }

    @Override
    protected FcxsPaymentPlan addValid(FcxsPaymentPlanVO entity) {
        FcxsOrder fcxsOrder = fcxsOrderService.fondByOrderIdOrOrderNo(entity.getOrderId(), entity.getOrderNo());
        if (fcxsOrder == null) {
            throw new RuntimeException("找不到订单");
        }
        FcxsPaymentPlan data = new FcxsPaymentPlan();
        BeanUtil.copyProperties(entity, data);
        data.setOrderId(fcxsOrder.getId());
        return data;
    }

    @Override
    protected FcxsPaymentPlan editValid(FcxsPaymentPlanUVO entity) {

        if (entity.getOrderId() != null) {
            FcxsPaymentPlan data = new FcxsPaymentPlan();
            FcxsOrder fcxsOrder = fcxsOrderService.fondByOrderId(entity.getOrderId());
            if (fcxsOrder == null) {
                throw new RuntimeException("找不到订单");
            }

            BeanUtil.copyProperties(entity, data);
            data.setOrderId(fcxsOrder.getId());
            return data;
        } else {
            return null;
        }


    }
}
